package com.movie.modules.jf.service.impl;

import com.movie.common.utils.NumberUtils;
import com.movie.modules.jf.entity.RelationEntity;
import com.movie.modules.jf.service.RelationService;
import com.movie.modules.jf.service.TypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.movie.common.utils.PageUtils;
import com.movie.common.utils.Query;

import com.movie.modules.jf.dao.NewsDao;
import com.movie.modules.jf.entity.NewsEntity;
import com.movie.modules.jf.service.NewsService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author JF-PJJ
 */
@Service("newsService")
@Transactional
public class NewsServiceImpl extends ServiceImpl<NewsDao, NewsEntity> implements NewsService {

    @Autowired
    private RelationService relationService;

    @Autowired
    private TypeService typeService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String title=params.get("title")==null?null:params.get("title").toString();
        Integer status= NumberUtils.objToInteger(params.get("status"));
        String releaseDateArr= (params.get("releaseDateArr") == null) ? null : params.get("releaseDateArr").toString();
        String beginDate= StringUtils.isNotEmpty(releaseDateArr)?releaseDateArr.split(",")[0]:null;
        String endDate=StringUtils.isNotEmpty(releaseDateArr)?releaseDateArr.split(",")[1]:null;
        IPage<NewsEntity> page = this.page(
                new Query<NewsEntity>().getPage(params),
                new QueryWrapper<NewsEntity>().like(StringUtils.isNotEmpty(title),"title",title).eq(status!=null,"status",status).between(StringUtils.isNotEmpty(releaseDateArr),"release_date",beginDate,endDate).orderByDesc("news_id")
        );

        return new PageUtils(page);
    }

    @Override
    public void update(NewsEntity news) {
        if(news.getNewsId()!=null&&news.getTypes()!=null&&news.getTypes().length>0){
      deleteByNewsId(news.getNewsId());
        }
        if(this.saveOrUpdate(news)&&news.getTypes()!=null&&news.getTypes().length>0){
            List<RelationEntity> list=new ArrayList<>();
            for(Integer typeId:news.getTypes()){
                RelationEntity relation=new RelationEntity();
                relation.setRelationType(3);
                relation.setResourceId(news.getNewsId());
                relation.setRelationId(typeId);
                list.add(relation);
            }
            relationService.saveBatch(list);
        }
    }

    @Override
    public void delete(Integer newsId) {
        if(this.removeById(newsId)){
            deleteByNewsId(newsId);
        }
    }

    @Override
    public Map<String, Object> getInfoById(Integer newsId) {
        Map<String,Object> map=new HashMap<>();
        map.put("news",this.getById(newsId));
        map.put("type",typeService.getByNewsId(newsId));
        return map;
    }

    private void deleteByNewsId(Integer newsId){
        relationService.remove(new QueryWrapper<RelationEntity>().eq("resource_id",newsId).eq("relation_type",3));
    }

}